home *** CD-ROM | disk | FTP | other *** search
/ Enter 2006 September / Enter 09 2006.iso / Internet / SpamExperts Home 1.1 / SpamExperts Home.exe / lib / spamexperts.modules / dns / rcode.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2006-07-14  |  2.8 KB  |  105 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.4)
  3.  
  4. '''DNS Result Codes.'''
  5. import dns.exception as dns
  6. NOERROR = 0
  7. FORMERR = 1
  8. SERVFAIL = 2
  9. NXDOMAIN = 3
  10. NOTIMP = 4
  11. REFUSED = 5
  12. YXDOMAIN = 6
  13. YXRRSET = 7
  14. NXRRSET = 8
  15. NOTAUTH = 9
  16. NOTZONE = 10
  17. BADVERS = 16
  18. _by_text = {
  19.     'NOERROR': NOERROR,
  20.     'FORMERR': FORMERR,
  21.     'SERVFAIL': SERVFAIL,
  22.     'NXDOMAIN': NXDOMAIN,
  23.     'NOTIMP': NOTIMP,
  24.     'REFUSED': REFUSED,
  25.     'YXDOMAIN': YXDOMAIN,
  26.     'YXRRSET': YXRRSET,
  27.     'NXRRSET': NXRRSET,
  28.     'NOTAUTH': NOTAUTH,
  29.     'NOTZONE': NOTZONE,
  30.     'BADVERS': BADVERS }
  31. _by_value = []([ (y, x) for x, y in _by_text.iteritems() ])
  32.  
  33. class UnknownRcode(dns.exception.DNSException):
  34.     '''Raised if an rcode is unknown.'''
  35.     pass
  36.  
  37.  
  38. def from_text(text):
  39.     '''Convert text into an rcode.
  40.     
  41.     @param text: the texual rcode
  42.     @type text: string
  43.     @raises UnknownRcode: the rcode is unknown
  44.     @rtype: int
  45.     '''
  46.     if text.isdigit():
  47.         v = int(text)
  48.         if v >= 0 and v <= 4095:
  49.             return v
  50.         
  51.     
  52.     v = _by_text.get(text.upper())
  53.     if v is None:
  54.         raise UnknownRcode
  55.     
  56.     return v
  57.  
  58.  
  59. def from_flags(flags, ednsflags):
  60.     '''Return the rcode value encoded by flags and ednsflags.
  61.  
  62.     @param flags: the DNS flags
  63.     @type flags: int
  64.     @param ednsflags: the EDNS flags
  65.     @type ednsflags: int
  66.     @raises ValueError: rcode is < 0 or > 4095
  67.     @rtype: int
  68.     '''
  69.     value = flags & 15 | ednsflags >> 20 & 4080
  70.     if value < 0 or value > 4095:
  71.         raise ValueError, 'rcode must be >= 0 and <= 4095'
  72.     
  73.     return value
  74.  
  75.  
  76. def to_flags(value):
  77.     '''Return a (flags, ednsflags) tuple which encodes the rcode.
  78.  
  79.     @param value: the rcode
  80.     @type value: int
  81.     @raises ValueError: rcode is < 0 or > 4095
  82.     @rtype: (int, int) tuple
  83.     '''
  84.     if value < 0 or value > 4095:
  85.         raise ValueError, 'rcode must be >= 0 and <= 4095'
  86.     
  87.     v = value & 15
  88.     ev = long(value & 4080) << 20
  89.     return (v, ev)
  90.  
  91.  
  92. def to_text(value):
  93.     '''Convert rcode into text.
  94.  
  95.     @param value: the rcode
  96.     @type value: int
  97.     @rtype: string
  98.     '''
  99.     text = _by_value.get(value)
  100.     if text is None:
  101.         text = str(value)
  102.     
  103.     return text
  104.  
  105.